package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.vo.SpecVo;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static com.dongdongshop.em.ResultEnum.ARGS_FAILE;

@Controller
@RequestMapping("specification")
public class SpecificationController {

    @DubboReference
    private SpecificationService specificationService;


    //查询规格
    @RequestMapping("findAll")
    @ResponseBody
    public AjaxResult findAll(String specName){
        try {
            List<TbSpecification> list = specificationService.findAll(specName);
            return AjaxResult.OK().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //增加方法
    @RequestMapping("addSpecAndOption")
    @ResponseBody
    public AjaxResult addSpecAndOption(TbSpecification specification,String specOptionJson ){
        List<TbSpecificationOption> optionList = JSONObject.parseArray(specOptionJson, TbSpecificationOption.class);
        try {
            if (specification == null && specOptionJson == null) {
                return AjaxResult.R(ARGS_FAILE);
            }
            specificationService.insertSpecAndOption(specification,optionList);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //回显操作
    @RequestMapping("editRole")
    @ResponseBody
    public AjaxResult editRole(Long id){
        try {
            if (id == null && id == 0){
                return AjaxResult.R(ResultEnum.ARGS_FAILE);
            }
            SpecVo vo = specificationService .specificationService(id);
            return AjaxResult.OK().setData(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();

        }
    }

    //修改方法
    @RequestMapping("updateSpec")
    @ResponseBody
    public AjaxResult updateSpec(TbSpecification specification,String specOptionJson ){
        List<TbSpecificationOption> optionList = JSONObject.parseArray(specOptionJson, TbSpecificationOption.class);
        try {
            if (specification == null && specOptionJson == null) {
                return AjaxResult.R(ARGS_FAILE);
            }
            specificationService.updateSpecAndOption(specification,optionList);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }

    }

    //删除方法
    @RequestMapping("deleteSpec")
    @ResponseBody
    public AjaxResult deleteBrand(Long[] ids){
        try {
            if (ids.length == 0){
                return AjaxResult.R(ARGS_FAILE);
            }
            specificationService.deleteBrand(ids);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
